package com.dianrui.fmi.fragment;

import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.BottomSheetDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.activity.AboutActivity;
import com.dianrui.fmi.activity.FeedBackActivity;
import com.dianrui.fmi.activity.HelpActivity;
import com.dianrui.fmi.event.ChangeName;
import com.dianrui.fmi.event.RfreshUserInfo;
import com.dianrui.fmi.order.MyOrderAcitivty;
import com.dianrui.fmi.activity.PersonalDataActivity;
import com.dianrui.fmi.activity.SettingsActivity;
import com.dianrui.fmi.activity.SimulatedTransactionNewActivity;
import com.dianrui.fmi.event.ExitLogin;
import com.dianrui.fmi.event.RfreshUserImg;
import com.dianrui.fmi.util.GlideUtil;
import com.dianrui.fmi.util.MyUtil;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.ToastUtil;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.share.Sharer;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.widget.ShareDialog;
import com.twitter.sdk.android.tweetcomposer.TweetComposer;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.net.MalformedURLException;
import java.net.URL;

import butterknife.BindView;
import butterknife.OnClick;

public class MyFragment extends BaseFragment {
    private CallbackManager callbackManager;
    private ShareDialog shareDialog;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.userimg)
    ImageView userimg;
    @BindView(R.id.username)
    TextView username;


    @Override
    public int getContentViewId() {
        return R.layout.frag_my;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        title.setText(getResources().getString(R.string.main_my_txt));
        if (!StringUtils.isEmpty(spUtils.getString("face"))) {
            GlideUtil.loadUserImageViewSize(getActivity(),spUtils.getString("face"),userimg);
        }
        if (!StringUtils.isEmpty(spUtils.getString("nickname"))) {
            username.setText(spUtils.getString("nickname"));
        }
        initFacebook();

    }

    @Override
    public void getData() {}


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ExitLogin exitLogin) {
        username.setText("Please Login");
        userimg.setImageResource(R.mipmap.default_user_icon);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RfreshUserImg rfreshUserImg) {
        if (!StringUtils.isEmpty(spUtils.getString("face"))) {
            GlideUtil.loadUserImageViewSize(getActivity(),spUtils.getString("face"),userimg);
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RfreshUserInfo rfreshUserInfo) {
        if (!StringUtils.isEmpty(spUtils.getString("nickname"))) {
            username.setText(spUtils.getString("nickname"));
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)//修改昵称后更新本地并上传给后台
    public void onMessageEvent(ChangeName changeName) {
        username.setText(changeName.name);
    }

    @OnClick({R.id.user_layout,R.id.invite_friends_layout, R.id.my_capital_layout, R.id.my_order_layout, R.id.simulating_trade_layout, R.id.help_layout, R.id.feedback_layout, R.id.contract_layout, R.id.about_layout, R.id.setting_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.user_layout:
                JumpActivitys(PersonalDataActivity.class);
                break;
            case R.id.invite_friends_layout:
                final BottomSheetDialog bottomSheetDialog=new BottomSheetDialog(getActivity());
                View showThanksMoneyview = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_invite_friends, null);
                Button cancelBtn=showThanksMoneyview.findViewById(R.id.cancel);
                LinearLayout facebookLayout=showThanksMoneyview.findViewById(R.id.facebook_layout);
                LinearLayout twitterLayout=showThanksMoneyview.findViewById(R.id.twitter_layout);
                facebookLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomSheetDialog.dismiss();
                        if (!MyUtil.checkfackboosInstalled(getActivity())) {
                            ToastUtil.showToast(getString(R.string.please_install_facebook));
                            return;
                        }
                        if (ShareDialog.canShow(ShareLinkContent.class)) {
                            ShareLinkContent linkContent = new ShareLinkContent.Builder()
                                    .setContentUrl(Uri.parse("https://developers.facebook.com"))
                                    .build();
                            shareDialog.show(linkContent);
                        }
                    }
                });
                twitterLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomSheetDialog.dismiss();
                        if (!MyUtil.checkfackboosInstalled(getActivity())) {
                            ToastUtil.showToast(getString(R.string.please_install_twitter));
                            return;
                        }
                        try {
                            TweetComposer.Builder builder = new TweetComposer.Builder(getActivity())
                                    .url(new URL("https://www.google.com/"));
                            builder.show();
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        }
                    }
                });
                cancelBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomSheetDialog.dismiss();
                    }
                });
                bottomSheetDialog.setContentView(showThanksMoneyview);
                bottomSheetDialog.setCanceledOnTouchOutside(true);
                if (!MyUtil.getActivity(bottomSheetDialog.getContext()).isFinishing()) {
                    bottomSheetDialog.show();
                }
                break;
            case R.id.my_capital_layout:

                break;
            case R.id.my_order_layout:
                JumpActivitys(MyOrderAcitivty.class);
                break;
            case R.id.simulating_trade_layout:
                startActivity(new Intent(getActivity(),SimulatedTransactionNewActivity.class).putExtra("pageType","1"));
                break;
            case R.id.help_layout:
                JumpActivitys(HelpActivity.class);
                break;
            case R.id.feedback_layout:
                JumpActivitys(FeedBackActivity.class);
                break;
            case R.id.contract_layout:
                Intent intent = new Intent(Intent.ACTION_SENDTO);
                intent.setData(Uri.parse("mailto:+cdi@021dr.cn"));
                intent.putExtra(Intent.EXTRA_SUBJECT, "title");
                intent.putExtra(Intent.EXTRA_TEXT, "content");
                startActivity(intent);
                break;
            case R.id.about_layout:
                JumpActivitys(AboutActivity.class);
                break;
            case R.id.setting_layout:
                JumpActivitys(SettingsActivity.class);
                break;
                default:
                    break;
        }
    }

    /**
     * facebook配置
     */
    private void initFacebook() {
        callbackManager = CallbackManager.Factory.create();
        shareDialog = new ShareDialog(this);
        // this part is optional
        shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {

            @Override
            public void onSuccess(Sharer.Result result) {
                //分享成功的回调，在这里做一些自己的逻辑处理
                ToastUtil.showToast("Share Success");
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {

            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //facebook的界面回调
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
